<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ include file="inc/taglibs.jsp" %>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <%@ include file="inc/meta.jsp" %>
    <meta name="description" content="">
    <meta name="author" content="">
    <title>反馈区域子级列表</title>
    <%@ include file="inc/css.jsp" %>
</head>

<body>
<div id="posts" class="wrapper">

    <%@ include file="inc/nav.jsp" %>

    <div id="page-wrapper">
        <div class="row">
            <div class="col-lg-12">
                <h1 class="page-header">反馈区域管理</h1>
            </div>
            <!-- /.col-lg-12 -->
        </div>

        <div class="row">
            <div class="col-lg-12">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <a href="javascript:void(0)" onclick="_areaChildList.fn.addInfo()" class="btn btn-outline btn-primary btn-lg" role="button">添加反馈区域</a>
                        <a href="javascript:void(0)" onclick="_areaChildList.fn.goBack()" class="btn btn-outline btn-success btn-lg" role="button">返回上级列表</a>
                        <input id="tempParentId" type="hidden" value="${parentId}"/>
                        <input id="tempLevel" type="hidden" value="${level}"/>
                        <input id="tempSize" type="hidden" value="${area.size}"/>
                    </div>
                    <!-- /.panel-heading -->
                    <div class="panel-body">

                        <div class="table-responsive">

                            <table class="table table-striped table-bordered table-hover" id="dataTables">
                                <colgroup>
                                    <col class="gradeA even" style="width: 10%"/>
                                    <col class="gradeA odd"/>
                                    <col class="gradeA even"/>
                                    <col class="gradeA odd"/>
                                    <col class="gradeA even"/>
                                    <col class="gradeA odd" style="width: 10%"/>
                                </colgroup>
                                <thead>
                                <tr>
                                    <th>编号</th>
                                    <th>名称</th>
                                    <th>总层级</th>
                                    <th>所在层级</th>
                                    <th>父级区域</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                </tbody>
                            </table>
                        </div>

                    </div>
                    <!-- /.panel-body -->

                </div>
                <!-- /.panel -->
            </div>
        </div>


    </div>
    <!-- /#page-wrapper -->

    <div class="modal fade" id="infoModal" tabindex="-1" role="dialog" aria-labelledby="pwdModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title">反馈区域详情</h4>
                </div>
                <div class="modal-body">
                    <label>反馈区域名称：</label>
                    <input class="form-control" id="tempName"/>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="button" onclick="_areaChildList.fn.subUpdateInfo()" class="btn btn-primary">确定</button>
                </div>
            </div>
            <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
    </div>

    <div class="modal fade" id="delModal" tabindex="-1" role="dialog" aria-labelledby="pwdModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title">删除提示</h4>
                </div>
                <div class="modal-body">
                    <input type="hidden" id="typeAreaId"/>
                    确定删除该反馈区域？
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="button" onclick="_areaChildList.fn.subDelInfo()" class="btn btn-primary">确定</button>
                </div>
            </div>
            <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
    </div>
</div>
<!-- /#wrapper -->

<%@ include file="inc/footer.jsp" %>
</body>

<script type="text/javascript">
    var _areaChildList = {
        v: {
            id: "_areaChildList",
            list: [],
            dTable: null
        },
        fn: {
            init: function () {
                _areaChildList.fn.dataTableInit();

                // 查询
                $("#c_search").click(function () {
                    _areaChildList.v.dTable.ajax.reload();
                });
            },
            dataTableInit: function () {
                _areaChildList.v.dTable = $leoman.dataTable($('#dataTables'), {
                    "processing": true,
                    "serverSide": true,
                    "searching": false,
                    "ordering": false,
                    "ajax": {
                        "url": "admin/area/list",
                        "type": "POST"
                    },
                    "columns": [
                        {"data": "id"},
                        {"data": "name"},
                        {"data": "size"},
                        {"data": "level"},
                        {"data": "parentName"},
                        {"data": ""}
                    ],
                    "columnDefs": [
                        {
                            "data": null,
                            "defaultContent": "<button type='button' title='编辑' style='margin-right: 8px;' class='btn btn-primary btn-circle edit'>" +
                            "<i class='fa fa-edit'></i>" +
                            "</button>" +
                            "<button type='button' title='下级区域' style='display: none;margin-right: 8px;' class='btn btn-info btn-circle children'>" +
                            "<i class='fa fa-table'></i>" +
                            "</button>" +
                            "<button type='button' title='删除' style='margin-right: 8px;' class='btn btn-danger btn-circle delete'>" +
                            "<i class='fa fa-remove'></i>" +
                            "</button>",
                            "targets": -1
                        }
                    ],
                    "createdRow": function (row, data, index) {
                        _areaChildList.v.list.push(data);
                    },
                    rowCallback: function (row, data) {
                        switch (Number(data.type)) {
                            case 1:
                                $('td', row).eq(1).html("协和老厂");
                                break;
                            case 2:
                                $('td', row).eq(1).html("全力工厂");
                                break;
                            case 3:
                                $('td', row).eq(1).html("泛洲中越");
                                break;
                        }

                        if (Number(data.size) > Number(data.level)) {
                            $('td', row).last().find(".children").css('display', '');
                        }

                        $('td', row).last().find(".edit").click(function () {
                            _areaChildList.fn.updateInfo(data);
                        });

                        $('td', row).last().find(".children").click(function () {
                            window.location.href = 'admin/area/indexInfo?parentId=' + data.id + '&level=' + data.level;
                        });

                        $('td', row).last().find(".delete").click(function () {
                            _areaChildList.fn.delInfo(data.id);
                        });
                    },
                    "fnServerParams": function (aoData) {
                        aoData.parentId = $('#tempParentId').val();
                        aoData.level = $('#tempLevel').val();
                    },
                    "fnDrawCallback": function (row) {
                        $leoman.uiform();
                    }
                });
            },
            addInfo: function () {
                $('#typeAreaId').val('');
                $('#tempName').val('');
                $("#infoModal").modal("show");
            },
            updateInfo: function (data) {
                $('#typeAreaId').val(data.id);
                $('#tempName').val(data.name);
                $("#infoModal").modal("show");
            },
            subUpdateInfo: function () {
                var typeAreaId = $('#typeAreaId').val();
                var typeName = $('#tempName').val();

                if (null == typeName || typeName == '') {
                    $leoman.notify('反馈区域名称不能为空', 'error');
                    return;
                }

                $leoman.ajax("admin/area/save", {
                    "id": typeAreaId,
                    "name": typeName,
                    "size": $('#tempSize').val(),
                    "parentId": $('#tempParentId').val(),
                    "level": $('#tempLevel').val()
                }, function (result) {
                    if (result == 1) {
                        $leoman.notify("操作成功", "success");
                        $("#infoModal").modal("hide");
                        _areaChildList.v.dTable.ajax.reload(null, false);
                    } else {
                        $leoman.notify("操作失败", "error");
                    }
                });
            },
            delInfo: function (id) {
                $('#typeAreaId').val(id);
                $("#delModal").modal("show");
            },
            subDelInfo: function () {
                var typeAreaId = $('#typeAreaId').val();

                $leoman.ajax("admin/area/delete", {
                    "id": typeAreaId
                }, function (result) {
                    if (result == 1) {
                        $leoman.notify("操作成功", "success");
                        $("#delModal").modal("hide");
                        _areaChildList.v.dTable.ajax.reload(null, false);
                    } else {
                        $leoman.notify("操作失败", "error");
                    }
                });
            },
            goBack: function () {
                var level = Number($('#tempLevel').val());
                if (level == 2) {
                    window.location.href = 'admin/area/index';
                } else {
                    window.location.href = 'admin/area/indexInfo?type=1&parentId=' + $('#tempParentId').val() + '&level=' + level;
                }
            }
        }
    }

    $(document).ready(function () {
        _areaChildList.fn.init();
    });

</script>

</html>